const app = getApp()
import {
    util,
    Toast,
    commonMixin
} from "../../utils/util.js";

Page({
    behaviors: [commonMixin],
    data: {
        show: true,
        taskQueue: [], //所有客户端发过来的待处理的任务
        webviewUrl: '', //链接
        queryUrl: '', //传递过来的原始地址
        shareData: {

        }
    },
    createToken() {
        return new Promise((resolve, reject) => {
            wx.showLoading({
                title: '登录中...',
                mask: true
            });
            util.http.get("applet/h5_auth", {}).then((res) => {
                wx.hideLoading();
                if (res.data.code == 0) {
                    resolve(res.data.data)
                } else {
                    resolve({
                        token: 'error_auth'
                    })
                }
            }).catch((err) => {
                wx.hideLoading();
                resolve({
                    token: 'error_auth'
                })
            })
        })
    },
    onLoad(options) {
        let userInfo = util.getUserInfo();

        if (options.url) {
            let queryUrl = decodeURIComponent(options.url);
            // console.log('原始路径：', queryUrl);

            //如果用户未绑定手机号 不允许参加活动
            if (!userInfo.phone) {
                wx.redirectTo({
                    url: '/pages/register/index?url=' + encodeURIComponent('/pages/webview/index?url=' + options.url)
                });
                return;
            }

            this.createToken().then((res) => {
                const realParams = util.objectToUrlParams(Object.assign({}, {
                    token: res.token
                }));
                //链接上本身就有参数的情况下
                const hash = queryUrl.indexOf('?') != -1 ? '&' : '?';
                let webview = queryUrl + hash + realParams;
                // console.log('添加用户信息后的路径：', webview);
                this.setData({
                    queryUrl: queryUrl,
                    webviewUrl: webview,
                })
            })
        }
    },
    onHide() {
        // this.setData({
        //     show: false,
        // });
    },
    onShow() {
        // this.setData({
        //     show: true,
        // });
    },
    bindmessage(e) {
        let array = e.detail.data || [];
        this.setData({
            taskQueue: array
        });
        this.taskQueueDo(array);
    },
    taskQueueDo() {

        let task = this.data.taskQueue[this.data.taskQueue.length - 1];
        if (task) {
            if (task.msgTpye && task.msgTpye == 'share') {
                let shareData = {};
                if (task.title) {
                    shareData.title = task.title;
                }
                if (task.path) {
                    shareData.path = '/pages/webview/index?url=' + (task.path);
                }

                if (task.imageUrl) {
                    shareData.imageUrl = task.imageUrl;
                }
                this.setData({
                    shareData: shareData
                })
            }
        }
    },
    bindload(e) {

    },
    binderror(e) {

    },
    onShareTimelineHONDA(){
        if (this.data.shareData && this.data.shareData.path) {
            // console.log('自定义分享', this.data.shareData)
            return this.data.shareData;
        }
        const queryPath = encodeURIComponent(this.data.queryUrl);
        return {
            title:'期待已久的活动终于上线了，必须分享一波！',
            path: '/pages/webview/index?url=' + queryPath
        }
    },
    //分享函数
    onShareAppMessage() {
        if (this.data.shareData && this.data.shareData.path) {
            // console.log('自定义分享', this.data.shareData)
            return this.data.shareData;
        }
        const queryPath = encodeURIComponent(this.data.queryUrl);
        return {
            title:'期待已久的活动终于上线了，必须分享一波！',
            path: '/pages/webview/index?url=' + queryPath
        }
    },
})